#include "Sphere.h"
#include "Image.h"

Sphere::Sphere(void)
{
	center = Point(0,2, -5);
	radius = 1.0f;
	color = Pixel(1.0f,0.0f, 0.0f);
}


Sphere::~Sphere(void)
{
}

bool Sphere::Hit(Point eye, Vec3f d, HitRecord *hit) const
{
	float A = dotProduct(d,d);
	float B = 2*(dotProduct(d, eye - center));
	float C = dotProduct(eye - center, eye - center) - radius * radius;
	float t;
	float det;
	if ((det = (B*B - 4*A*C) )< 0 )
		return false;
	else
	{
		float z = sqrtf(det);
		if ((t = ((-1*B - z)/(2*A))) > 0)
		{
			if (t < hit->t)
			{
				hit->surface = (Surface*)this;
				hit->t = t;
				return true;
			}
		}
		if ((t = ((-1*B + z)/(2*A)) > 0))
		{
			if (t < hit->t)
			{
				hit->surface = (Surface*)this;
				hit->t = t;
				return true;
			}
		}
	}
	return false;
}		

Pixel Sphere::Shade(Point loc, Vec3f in) const
{
	return color;
}